package top.hooray.delayedpayment.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;
import top.hooray.delayedpayment.entity.OrderStatus;
import top.hooray.delayedpayment.service.OrderService;

/**
 * @author hooray
 * @date 2021/3/9 23:46
 */
@Component
public class CancelOrderListener implements ChannelAwareMessageListener {

    private final OrderService orderService;

    public CancelOrderListener(OrderService orderService) {
        this.orderService = orderService;
    }

    @Override
    @RabbitListener(queues = "queue.dlx", ackMode = "MANUAL")
    public void onMessage(Message message, Channel channel) throws Exception {
        Long orderId = Long.valueOf(new String(message.getBody()));
        orderService.updateOrderStatus(orderId, OrderStatus.CANCELED);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
